// pages/list/index.js
const db=wx.cloud.database()
const question_db=db.collection('question')

Page({
  data: {
    total: 0,
    answer: "A",
    current: 1, //从第一题开始
    isSelect: false,
    questionResult: [],
    titles:[]
  },


  onLoad() {
    var info= wx.getStorageSync('info')
    var job = info[2]
    var version=wx.getStorageSync('bb')
    question_db.where({"_id":version})
  .get()
  .then(res=>{
    console.log(res.data[0].question[0]['regular'])
    this.setData({
      titles:res.data[0].question[0]['regular']
    })
    var subject = this.data.titles[0]
    console.log("subject", subject)
    this.setData({ 
      subject: subject,
      total: this.data.titles.length
    })
})
var getQR=wx.getStorageSync('QR')
this.setData({
  getQR:getQR
})
    var that=this
    that.setData({
      id:that.options.id,
    })
    if(that.data.id){
      that.setData({
        current:parseInt(that.data.id),
        subject:this.data.titles[that.data.id-1],
        userxz:that.data.getQR[that.data.id-1].select
      })
    }
  },
  next(){
    if (this.data.current < this.data.total) {
      this.setData({
        current: this.data.current + 1,
        subject: this.data.titles[this.data.current],
        userxz:this.data.getQR[this.data.current].select
      })
    }
    
  },
  last(){
      if (this.data.current > 1) {
        this.setData({
          current: this.data.current -1,
          
        })
        this.setData({
          subject: this.data.titles[this.data.current-1],
          userxz:this.data.getQR[this.data.current-1].select
        })
      }
  },
  submit(e) {
    if(!this.data.getQR){
    console.log(e)
    let userSelcet = e.target.dataset.select
    if (userSelcet !== undefined) {
      this.data.questionResult.push({
        "questionId": this.data.current,
        "select": userSelcet
      }) // 记录
      // 不是最后一题，current+1,并进行下一题
  
     
    
    //  this.setData({
    //    userxz:getQR[this.data.current]
    //  })
      if (this.data.current < this.data.total) {
        this.setData({
          current: this.data.current + 1,
          subject: this.data.titles[this.data.current]
        })
      }
      // 如果已经到了最后一题
      if (this.data.current === this.data.total) {
        wx.showToast({
          icon: 'none',
          title: '已经最后一道题啦',
        })
      }
      // 答完最后一道题
      if (this.data.current === this.data.total && this.data.questionResult.length === this.data.total) {
        console.log("恭喜你全部答完")
        console.log(this.data.questionResult)
        wx.navigateTo({
          url: '../part2/review',
        })
        wx.setStorageSync('QR', this.data.questionResult)
      }
    }
  }
  else{

    console.log(e)
    let userSelcet = e.target.dataset.select
    if (userSelcet !== undefined) {
      this.data.getQR[this.data.current-1]={
        "questionId": this.data.current,
        "select": userSelcet
      }
      console.log(this.data.getQR)
      wx.setStorageSync('QR', this.data.getQR)
      var getQR=wx.getStorageSync('QR')
      this.setData({
        getQR:getQR,
        userxz:this.data.getQR[this.data.current].select
      })
      if (this.data.current < this.data.total) {
        this.setData({
          current: this.data.current + 1,
          subject: this.data.titles[this.data.current]
        })
      }
    }
  }
  },

})


